package jpaperwork.meta.descriptor;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

/**
 * User: fleipold
 * Date: Jun 30, 2009
 * Time: 3:38:12 PM
 */
public class Form implements Type{
    private List<Field> fields = new ArrayList<Field>();
    private List<FormDecoration> decorations = new ArrayList<FormDecoration>();

    public Form(List<Field> fields) {
        this.fields = fields;
    }

    public Form(List<Field> fields, FormDecoration... decorations) {
        this(fields);
        this.decorations.addAll(asList(decorations));
    }


    public Form() {
    }

    public void addField(Field field){
        this.fields.add(field);
    }

    public List<Field> getFields() {
        return fields;
    }

    public boolean addDecoration(FormDecoration formDecoration) {
        return decorations.add(formDecoration);
    }

    public <T extends FormDecoration> T getDecoration(Class<T> decorationClass){
        for (FormDecoration decoration : decorations){
            if (decorationClass.isAssignableFrom(decoration.getClass())){
                return (T) decoration;
            }
        }
        throw new IllegalArgumentException("No decoration with class "+ decorationClass.getName());
    }
}
